package cisdi.mes.sm.rm.serviceImpl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cisdi.mes.sm.rm.dao.StorageContentDao;
import cisdi.mes.sm.rm.domain.SmRmMeasureDocLog;
import cisdi.mes.sm.rm.domain.SmRmStorageContent;
import cisdi.mes.sm.rm.service.StockWgtCheckMgr;
import cisdi.mes.sm.util.base.Arith;
import cisdi.mes.sm.util.dao.BaseDao;

@Service
public class StockWgtCheckMgrImpl implements StockWgtCheckMgr {
	@Autowired
	private StorageContentDao storageContentDao;
	@Autowired
	private BaseDao baseDao;
	public StorageContentDao getStorageContentDao() {
		return storageContentDao;
	}

	public void setStorageContentDao(StorageContentDao storageContentDao) {
		this.storageContentDao = storageContentDao;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	@Override
	public short checkStockWgt(String storageId, String materialId,
			Double weight,HttpServletRequest request) {
		short rtn = 0;// 代表成功	
		List<SmRmStorageContent> smRmStorageContents = storageContentDao
				.findByStorageIdAndMaterialId(storageId, materialId);
		if (smRmStorageContents != null && smRmStorageContents.size() > 0) {
			Double totalWgt = getTotalWgt(smRmStorageContents);
			if (!totalWgt.equals(weight)) {
				Double diff;
				if (weight < totalWgt) {
					diff = Arith.sub(totalWgt,weight);
					reduceStockWeight(smRmStorageContents, diff,  request);
				} else {
					diff =  Arith.sub(weight,totalWgt);
					int size = smRmStorageContents.size();
					SmRmStorageContent smRmStorageContent = smRmStorageContents.get(size-1);
					increaseStockWeight(smRmStorageContent,diff,  request);
				}
			} else {
				rtn = 2; // 盘库重量等于料仓现有重量
			}
		} else {
			rtn = 1; // 料仓中没有可供盘库的物料
		}
		return rtn;
	}

	private Double getTotalWgt(List<SmRmStorageContent> smRmStorageContents) {
		Double totalWgt = 0d;
		for (SmRmStorageContent smRmStorageContent : smRmStorageContents) {
			totalWgt = totalWgt + smRmStorageContent.getWeight();
		}
		return totalWgt;
	}

	private void reduceStockWeight(
			List<SmRmStorageContent> smRmStorageContents, Double diff,HttpServletRequest request) {
		for (SmRmStorageContent smRmStorageContent : smRmStorageContents) {
			SmRmMeasureDocLog log = new SmRmMeasureDocLog();
			log.setIdAddr(request.getRemoteAddr());
			log.setMeasureId(smRmStorageContent.getMeasureId());
			log.setMaterialId(smRmStorageContent.getMaterialId());
			log.setEvent("INVENTORY_LOSSES");
			log.setFromStorageId(smRmStorageContent.getStorageId());
			if (smRmStorageContent.getWeight() <= diff) {
				diff = diff - smRmStorageContent.getWeight();
				baseDao.delete(smRmStorageContent);
				log.setWeight(smRmStorageContent.getWeight());
			} else {
				smRmStorageContent.setWeight(smRmStorageContent.getWeight()
						- diff);
				baseDao.update(smRmStorageContent);
				log.setWeight(diff);
				diff = 0d;
			}
			baseDao.save(log);
			if (diff.doubleValue() == 0)
				break;
		}
	}

	private void increaseStockWeight(SmRmStorageContent smRmStorageContent,
			Double diff,HttpServletRequest request) {
		smRmStorageContent.setWeight(smRmStorageContent.getWeight() + diff);
		baseDao.update(smRmStorageContent);
		SmRmMeasureDocLog log = new SmRmMeasureDocLog();
		log.setIdAddr(request.getRemoteAddr());
		log.setMeasureId(smRmStorageContent.getMeasureId());
		log.setMaterialId(smRmStorageContent.getMaterialId());
		log.setEvent("INVENTORY_PROFIT");
		log.setFromStorageId(smRmStorageContent.getStorageId());
		log.setWeight(diff);
		baseDao.save(log);

	}

}
